using Agron.Data.Contracts;
using Agron.Services.Behaviors;

using System;
using System.Web.Security;
using System.Collections.Generic;
using System.Web.UI.WebControls;


namespace Agron.Web.Forms
{
    /// <summary>
    /// Code-Behind for the <c>ProfileForm.aspx</c> web form.
    /// </summary>
    public partial class ProfileForm : System.Web.UI.Page
    {
        private const string UserProfileKey = "AgronUserProfile";

        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                if ((null == base.User) || (null == base.User.Identity) || (string.IsNullOrEmpty(base.User.Identity.Name)))
                {
                    base.Response.Redirect("~/Forms/LoginForm.aspx");
                }
                else
                {
                    this.GetUserProfile();

                    this.userProfileDetailsView.DataSource = base.ViewState[ProfileForm.UserProfileKey];
                    base.DataBind();
                }
            }
        }

        /// <summary>
        /// Handles the ModeChanging event of the userProfileDetailsView control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.DetailsViewModeEventArgs"/> instance containing the event data.</param>
        protected void userProfileDetailsView_ModeChanging(object sender, DetailsViewModeEventArgs e)
        {
            this.userProfileDetailsView.DataSource = base.ViewState[ProfileForm.UserProfileKey];
            this.userProfileDetailsView.ChangeMode(e.NewMode);

            base.DataBind();
        }

        /// <summary>
        /// Handles the ItemUpdating event of the userProfileDetailsView control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Web.UI.WebControls.DetailsViewUpdateEventArgs"/> instance containing the event data.</param>
        protected void userProfileDetailsView_ItemUpdating(object sender, DetailsViewUpdateEventArgs e)
        {
            UserProfile newProfile = new UserProfile();
            newProfile.DisplayName = e.NewValues["DisplayName"].ToString();
            newProfile.Email = e.NewValues["Email"].ToString();

            MembershipService membershipService = new MembershipService();
            membershipService.UpdateUserProfile(newProfile);
        }

        private void GetUserProfile()
        {
            List<UserProfile> dataList = new List<UserProfile>();
            MembershipService membershipService = new MembershipService();

            MembershipUser membershipUser = Membership.GetUser();
            dataList.Add(membershipService.GetUserProfileByUserName(membershipUser.UserName));

            base.ViewState[ProfileForm.UserProfileKey] = dataList;
        }
    }
}
